package com.zt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zt.annotation.AdminLog;
import com.zt.common.Result;
import com.zt.dto.CouponCreateRequest;
import com.zt.dto.CouponQueryRequest;
import com.zt.service.AdminCouponsService;
import com.zt.vo.CouponVO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券管理控制器
 */
@RestController
@RequestMapping("/api/admin/coupons")
@Slf4j
@Validated
public class CouponController {

    @Autowired
    private AdminCouponsService couponsService;

    /**
     * 分页查询优惠券
     */
    @GetMapping("/page")
    public Result<IPage<CouponVO>> pageCoupons(CouponQueryRequest request) {
        try {
            IPage<CouponVO> page = couponsService.pageCoupons(request);
            return Result.success("查询成功", page);
        } catch (Exception e) {
            log.error("查询优惠券列表失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询优惠券列表 (POST方式)
     */
    @PostMapping("/list")
    public Result<IPage<CouponVO>> listCoupons(@RequestBody CouponQueryRequest request) {
        try {
            IPage<CouponVO> page = couponsService.pageCoupons(request);
            return Result.success("查询成功", page);
        } catch (Exception e) {
            log.error("查询优惠券列表失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 创建优惠券
     */
    @PostMapping
    @AdminLog(module = "优惠券管理", operation = "创建", description = "创建优惠券", type = AdminLog.OperationType.CREATE)
    public Result<Integer> createCoupon(@Valid @RequestBody CouponCreateRequest request) {
        try {
            Integer couponId = couponsService.createCoupon(request);
            return Result.success("创建成功", couponId);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("创建优惠券失败", e);
            return Result.error("创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新优惠券
     */
    @PutMapping("/{id}")
    @AdminLog(module = "优惠券管理", operation = "修改", description = "修改优惠券信息", type = AdminLog.OperationType.UPDATE)
    public Result<Boolean> updateCoupon(@PathVariable @NotNull Integer id,
                                       @Valid @RequestBody CouponCreateRequest request) {
        try {
            Boolean success = couponsService.updateCoupon(id, request);
            return success ? Result.success("更新成功", true) : Result.error("更新失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("更新优惠券失败: {}", id, e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除优惠券
     */
    @DeleteMapping("/{id}")
    @AdminLog(module = "优惠券管理", operation = "删除", description = "删除优惠券", type = AdminLog.OperationType.DELETE)
    public Result<Boolean> deleteCoupon(@PathVariable @NotNull Integer id) {
        try {
            Boolean success = couponsService.deleteCoupon(id);
            return success ? Result.success("删除成功", true) : Result.error("删除失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("删除优惠券失败: {}", id, e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }

    /**
     * 获取优惠券详情
     */
    @GetMapping("/{id}")
    public Result<CouponVO> getCouponDetail(@PathVariable @NotNull Integer id) {
        try {
            CouponVO coupon = couponsService.getCouponDetail(id);
            return coupon != null ? Result.success("查询成功", coupon) : Result.error("优惠券不存在");
        } catch (Exception e) {
            log.error("查询优惠券详情失败: {}", id, e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 激活优惠券
     */
    @PostMapping("/{id}/activate")
    @AdminLog(module = "优惠券管理", operation = "激活", description = "激活优惠券", type = AdminLog.OperationType.UPDATE)
    public Result<Boolean> activateCoupon(@PathVariable @NotNull Integer id) {
        try {
            Boolean success = couponsService.activateCoupon(id);
            return success ? Result.success("激活成功", true) : Result.error("激活失败");
        } catch (Exception e) {
            log.error("激活优惠券失败: {}", id, e);
            return Result.error("激活失败: " + e.getMessage());
        }
    }

    /**
     * 停用优惠券
     */
    @PostMapping("/{id}/deactivate")
    @AdminLog(module = "优惠券管理", operation = "停用", description = "停用优惠券", type = AdminLog.OperationType.UPDATE)
    public Result<Boolean> deactivateCoupon(@PathVariable @NotNull Integer id) {
        try {
            Boolean success = couponsService.deactivateCoupon(id);
            return success ? Result.success("停用成功", true) : Result.error("停用失败");
        } catch (Exception e) {
            log.error("停用优惠券失败: {}", id, e);
            return Result.error("停用失败: " + e.getMessage());
        }
    }

    /**
     * 获取有效优惠券列表
     */
    @GetMapping("/active")
    public Result<List<CouponVO>> getActiveCoupons() {
        try {
            List<CouponVO> coupons = couponsService.getActiveCoupons();
            return Result.success("查询成功", coupons);
        } catch (Exception e) {
            log.error("查询有效优惠券失败", e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除优惠券
     */
    @DeleteMapping("/batch")
    @AdminLog(module = "优惠券管理", operation = "批量删除", description = "批量删除优惠券", type = AdminLog.OperationType.DELETE)
    public Result<Boolean> batchDeleteCoupons(@RequestBody @NotEmpty List<Integer> ids) {
        try {
            Boolean success = couponsService.batchDeleteCoupons(ids);
            return success ? Result.success("批量删除成功", true) : Result.error("批量删除失败");
        } catch (Exception e) {
            log.error("批量删除优惠券失败: {}", ids, e);
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 手动更新过期优惠券状态
     */
    @PostMapping("/update-expired")
    public Result<Boolean> updateExpiredCoupons() {
        try {
            couponsService.updateExpiredCoupons();
            return Result.success("更新成功", true);
        } catch (Exception e) {
            log.error("更新过期优惠券状态失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }
}
